package com.onemile.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.onemile.entity.Session;

import android.util.Log;

public class UtiIO
{
   public static String ConvertStreamToString(InputStream is)
   {
	  BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	  StringBuilder sb = new StringBuilder();
	  String line = null;
	  try
	  {
		 while ((line = reader.readLine()) != null)
		 {
			sb.append(line + " ");
		 }
	  }
	  catch (IOException e)
	  {
		 e.printStackTrace();
	  }
	  finally
	  {
		 try
		 {
			is.close();
		 }
		 catch (IOException e)
		 {
			e.printStackTrace();
		 }
	  }
	  return sb.toString();
   }

   public static String InputStreamToString(InputStream in) throws IOException
   {

	  StringBuffer out = new StringBuffer();
	  byte[] b = new byte[4096];
	  int n;
	  while ((n = in.read(b)) != -1)
	  {
		 out.append(new String(b, 0, n));
	  }
	  // Log.i("String的长度",new Integer(out.length()).toString());
	  return out.toString();
   }

   public static byte[] InputStreamToByte(InputStream is) throws IOException
   {
	  ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
	  int ch;
	  while ((ch = is.read()) != -1)
	  {
		 bytestream.write(ch);
	  }
	  byte imgdata[] = bytestream.toByteArray();
	  bytestream.close();
	  return imgdata;
   }
   
   /**
    * Convert byte[] to hex
    * string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)来转换成16进制字符串。
    * 
    * @param src
    *           byte[] data
    * @return hex string
    */
   public static String bytesToHexString(byte[] src)
   {
	  try
	  {
		 StringBuilder stringBuilder = new StringBuilder("");
		 if (src == null || src.length <= 0)
		 {
			return null;
		 }
		 for (int i = 0; i < src.length; i++)
		 {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2)
			{
			   stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		 }
		 return stringBuilder.toString();
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiIO_bytesToHexString(src) ", e);
		 }
	  }
	  return null;
   }

   /**
    * Convert hex string to byte[]
    * 
    * @param hexString
    *           the hex string
    * @return byte[]
    */
   public static byte[] hexStringToBytes(String hexString)
   {
	  try
	  {
		 hexString = hexString.toUpperCase();
		 int length = hexString.length() / 2;
		 char[] hexChars = hexString.toCharArray();
		 byte[] d = new byte[length];
		 for (int i = 0; i < length; i++)
		 {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		 }
		 return d;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiIO_hexStringToBytes(hexString) ", e);
		 }
	  }
	  if (hexString == null || hexString.equals(""))
	  {
		 return null;
	  }
	  return null;
   }

   /**
    * Convert char to byte
    * 
    * @param c
    *           char
    * @return byte
    */
   private static byte charToByte(char c)
   {
	  return (byte) "0123456789ABCDEF".indexOf(c);
   }

   // 将指定byte数组以16进制的形式打印到控制台
   public static void printHexString(byte[] b)
   {
	  for (int i = 0; i < b.length; i++)
	  {
		 String hex = Integer.toHexString(b[i] & 0xFF);
		 if (hex.length() == 1)
		 {
			hex = '0' + hex;
		 }
		 System.out.print(hex.toUpperCase());
	  }

   }

   public static String bytes2HexString(byte[] b)
   {
	  String ret = "";
	  for (int i = 0; i < b.length; i++)
	  {
		 String hex = Integer.toHexString(b[i] & 0xFF);
		 if (hex.length() == 1)
		 {
			hex = '0' + hex;
		 }
		 ret += hex.toUpperCase();
	  }
	  return ret;
   }
}
